import SimpleAnimateProperties from "./animations/SimpleAnimateProperties";
import StageBase from "./framework/StageBase";
import SceneManager from "./framework/SceneManager";
import EventManager from "./EventManager";
import AudioManager from "./AudioManager";
import App from "./framework/App";

/**
 * 简单特效工具，用于快速在特定位置快速产生小特效
 */
export default class SimpleEffect extends cc.Component {

    private currStage: StageBase = null;
    private particlesCount: number = 0;

    static get newInstance(): SimpleEffect {
        let node = new cc.Node('SimpleEffect');
        let instance = node.addComponent(SimpleEffect);
        cc.Canvas.instance.node.addChild(node);
        node.position = cc.Vec2.ZERO;
        return instance;
    }

    onDestroy() {
        EventManager.removeEvent(SceneManager.Event_StageOut, this.onStageOut, this);
    }

    onLoad() {
        EventManager.addEvent(SceneManager.Event_StageOut, this.onStageOut, this);
    }

    start() {
        this.currStage = SceneManager.currStage;
    }

    onStageOut(view: StageBase) {
        if (this.currStage == view && this.node) {
            this.node.destroy();
        }
    }

    /**
     * 飘星星
     */
    starts(position: cc.Vec2, amount: number, radius: number, duration: number) {
        AudioManager.playFX('common/fx_LittleHappy');
        for (let i = 0; i < amount; i++) {
            let delay = Math.random() * 0.3;
            this.scheduleOnce(() => {
                let particle: cc.Node = cc.instantiate(App.instance.resPreloader.getRes('prefabs/common/Star') as cc.Prefab);
                this.node.addChild(particle);
                this.particlesCount += 1;
                let pro = particle.addComponent(SimpleAnimateProperties);
                pro.destroyOnStageSwap(true);
                pro.n = new Date().getTime();
                this.oneFlash(pro, position, radius, duration);
            }, delay);
        }
    }

    private oneFlash(particlePro: SimpleAnimateProperties, position: cc.Vec2, radius: number, duration: number) {
        let originPos = cc.v2(position.x + (Math.random() * 2 - 1) * radius * 0.5, position.y + (Math.random() * 2 - 1) * radius * 0.5);
        particlePro.node.setPosition(originPos);
        particlePro.node.opacity = 255;
        particlePro.node.angle = Math.random() * 360;
        particlePro.node.scale = 0.2 + Math.random();
        cc.tween(particlePro.node).to(0.3 + Math.random() * 0.3, { opacity: 0 }).call(() => {
            if (new Date().getTime() - particlePro.n < duration * 1000) {
                this.oneFlash(particlePro, position, radius, duration);
            } else {
                particlePro.node.destroy();
                this.particlesCount -= 1;
                if (this.particlesCount == 0) {
                    //等待1秒钟再无粒子生成，则删除
                    this.scheduleOnce(() => {
                        if (this.particlesCount == 0) {
                            this.node.destroy();
                        }
                    }, 1);
                }
            }
        }).start();
    }

}